package zoltan.balog.result.impl;

import java.util.Map;

import zoltan.balog.entities.City;
import zoltan.balog.entities.Route;
import zoltan.balog.plan.IPlan;
import zoltan.balog.result.IResult;

public class Result implements IResult {

	private final IPlan plan;
	private final Map<Route, City> solution;

	public Result(final Map<Route, City> solution, final IPlan plan) {
		super();
		this.solution = solution;
		this.plan = plan;
	}

	private double countOptimum() {
		final double[] expense = new double[plan.getNumOfCities()];
		
		for (int i = 0; i < plan.getNumOfCities(); i++) {
			expense[i] = 0.0;
		}
		
		for (final Map.Entry<Route, City> entry : solution.entrySet()) {
			expense[entry.getValue().getId()] += entry.getKey().getCost();
		}

		double max = Double.MIN_VALUE;
		for (int i = 0; i < plan.getNumOfCities(); i++) {
			if (expense[i] > max) {
				max = expense[i];
			}
		}

		return max;
	}

	@Override
	public double getOptimum() {
		return countOptimum();
	}

	@Override
	public Map<Route, City> getSolution() {
		return solution;
	}

	@Override
	public String toString() {
		return "Result Optimum=" + getOptimum() + System.getProperty("line.separator") + "[solution=" + solution
				+ System.getProperty("line.separator") + "plan=" + plan + "]";
	}
}
